package com.zekdot.userclient.util;

/**
 * Create by zekdot on 20-1-7.
 */

import com.zekdot.userclient.entity.UserEntity;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取字符串的md5摘要
 *
 * @author zekdot
 */
public class PasswordUtil {

    private static String salt="register";   //盐

    /**
     * 获取MD5
     * @param value
     * @return
     */
    public static String getMd5(String value) {
        value=new StringBuffer().append(salt).append(value).toString(); //加盐做md5
        return DigestUtils.md5DigestAsHex(value.getBytes());
    }
    /**
     * 获取用户的token
     * @param userEntity
     * @param ttlMills
     * @return
     */
    public static String getToken(UserEntity userEntity,long ttlMills) throws UnsupportedEncodingException {
        String token="";
        Map<String,Object> claimMap = new HashMap<>();
        claimMap.put("identify",userEntity.getIdentify());
        claimMap.put("joinMet",userEntity.getJoinMet());
        token = JwtUtil.createJWT("identToken",userEntity.getUsername(),claimMap,ttlMills);
        return token;
    }
}
